﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubSaveData : MonoBehaviour
{
        //默认值
        public const float explode_speed_default = 120f;
        public const float explode_alphaCube_default = 80f;
        public const float explode_scaleCube_default = 1f;
        public const float explode_positionCube_default = 10f;
        public const float explode_alphaTag_default = 200f;
        public const float explode_scaleTag_default = 2f;
        public const float explode_positionTag_default = 30f;

        public const float rotate_speed_default = 34f;
        public const bool rotate_isClockWise_default = true;
        public const bool rotate_isRotateX_default = true;
        public const bool rotate_isRotateY_default = true;
        public const bool rotate_isRotateZ_default = true;

        public const bool music_enableMusic_default = true;
        public const float music_volMusic_default = 80f;

        [Header("控制旋转的脚本")]
        public CubRotate cr;

        [Header("控制炸开的脚本")]
        public CubExplode ce;

        //键值名
        private const string ROTATE_SPEED = "rotate_speed";
        private const string ROTATE_C = "rotate_c";
        private const string ROTATE_X = "rotate_x";
        private const string ROTATE_Y = "rotate_y";
        private const string ROTATE_Z = "rotate_z";

        private const string EXPLODE_SPEED = "explode_speed";

        private const string EXPLODE_DEFAULT_ALPHA = "explode_default_alpha";
        private const string EXPLODE_DEFAULT_SCALE = "explode_default_scale";
        private const string EXPLODE_DEFAULT_POSITION = "explode_default_position";

        private const string EXPLODE_TAG_ALPHA = "explode_tag_alpha";
        private const string EXPLODE_TAG_SCALE = "explode_tag_scale";
        private const string EXPLODE_TAG_POSITION = "explode_tag_position";

        private const string MUSIC_ENABLE = "music_enable_music";
        private const string MUSIC_VOL = "music_vol_music";


        //备份值
        private float explode_speed;
        private float explode_alphaCube;
        private float explode_scaleCube;
        private float explode_positionCube;
        private float explode_alphaTag;
        private float explode_scaleTag;
        private float explode_positionTag;

        private float rotate_speed;
        private bool rotate_isClockWise;
        private bool rotate_isRotateX;
        private bool rotate_isRotateY;
        private bool rotate_isRotateZ;

        private bool music_enableMusic;
        private float music_volMusic;

        //bool-》int
        private const int trueVal = 1;
        private const int falseVal = 0;

        void Awake()
        {
                Load();
                BackupVal();
        }
        /// <summary>
        /// 备份值
        /// </summary>
        public void BackupVal()
        {
                rotate_speed = cr.speed;
                rotate_isClockWise = cr.isClockWise;
                rotate_isRotateX = cr.isRotateX;
                rotate_isRotateY = cr.isRotateY;
                rotate_isRotateZ = cr.isRotateZ;

                explode_speed = ce.speed;
                explode_alphaCube = ce.alphaCube;
                explode_scaleCube = ce.scaleCube;
                explode_positionCube = ce.positionCube;
                explode_alphaTag = ce.alphaTag;
                explode_scaleTag = ce.scaleTag;
                explode_positionTag = ce.positionTag;
        }
        /// <summary>
        /// 加载值
        /// </summary>
        public void Load()
        {
                if (cr)
                {
                        cr.speed = PlayerPrefs.GetFloat(ROTATE_SPEED, rotate_speed_default);

                        int Val = BoolToInt(rotate_isClockWise_default);
                        cr.isClockWise = IntToBool(PlayerPrefs.GetInt(ROTATE_C, Val));

                        Val = BoolToInt(rotate_isRotateX_default);
                        cr.isRotateX = IntToBool(PlayerPrefs.GetInt(ROTATE_X, Val));

                        Val = BoolToInt(rotate_isRotateY_default);
                        cr.isRotateY = IntToBool(PlayerPrefs.GetInt(ROTATE_Y, Val));

                        Val = BoolToInt(rotate_isRotateZ_default);
                        cr.isRotateZ = IntToBool(PlayerPrefs.GetInt(ROTATE_Z, Val));
                }

                if (ce)
                {
                        ce.speed = PlayerPrefs.GetFloat(EXPLODE_SPEED, explode_speed_default);

                        ce.alphaCube = PlayerPrefs.GetFloat(EXPLODE_DEFAULT_ALPHA, explode_alphaCube_default);
                        ce.scaleCube = PlayerPrefs.GetFloat(EXPLODE_DEFAULT_SCALE, explode_scaleCube_default);
                        ce.positionCube = PlayerPrefs.GetFloat(EXPLODE_DEFAULT_POSITION, explode_positionCube_default);

                        ce.alphaTag = PlayerPrefs.GetFloat(EXPLODE_TAG_ALPHA, explode_alphaTag_default);
                        ce.scaleTag = PlayerPrefs.GetFloat(EXPLODE_TAG_SCALE, explode_scaleTag_default);
                        ce.positionTag = PlayerPrefs.GetFloat(EXPLODE_TAG_POSITION, explode_positionTag_default);
                }
        }
        /// <summary>
        /// 保存值
        /// </summary>
        public void Save()
        {
                if (cr)
                {
                        BackupAVal(ROTATE_SPEED,ref rotate_speed, cr.speed);

                        BackupAVal(ROTATE_C, ref rotate_isClockWise, cr.isClockWise);

                        BackupAVal(ROTATE_X, ref rotate_isRotateX, cr.isRotateX);
                        BackupAVal(ROTATE_Y, ref rotate_isRotateY, cr.isRotateY);
                        BackupAVal(ROTATE_Z, ref rotate_isRotateZ, cr.isRotateZ);
                }

                if (ce)
                {
                        BackupAVal(EXPLODE_SPEED, ref explode_speed,  ce.speed);

                        BackupAVal(EXPLODE_DEFAULT_ALPHA, ref explode_alphaCube,  ce.alphaCube);
                        BackupAVal(EXPLODE_DEFAULT_SCALE, ref explode_scaleCube,  ce.scaleCube);
                        BackupAVal(EXPLODE_DEFAULT_POSITION, ref explode_positionCube, ce.positionCube);

                        BackupAVal(EXPLODE_TAG_ALPHA, ref explode_alphaTag, ce.alphaTag);
                        BackupAVal(EXPLODE_TAG_SCALE, ref explode_scaleTag,  ce.scaleTag);
                        BackupAVal(EXPLODE_TAG_POSITION, ref explode_positionTag,  ce.positionTag);
                }
        }

        private void BackupAVal(string keyName, ref float backupVal, float newVal)
        {
                if (backupVal != newVal)
                {
                        PlayerPrefs.SetFloat(keyName, newVal);
                        backupVal = newVal;
                }
        }

        private void BackupAVal(string keyName, ref bool backupVal, bool newVal)
        {


                if (backupVal != newVal)
                {
                        PlayerPrefs.SetInt(keyName, BoolToInt(newVal));
                        backupVal = newVal;
                }
        }

        private int BoolToInt(bool b)
        {
                if (b)
                {
                        return trueVal;
                }
                else
                {
                        return falseVal;
                }
        }

        private bool IntToBool(int i)
        {
                if (i != trueVal)
                {
                        return false;
                }
                else
                {
                        return true;
                }
        }
}
